package com.fjeld.middleware.activemq;

import com.fjeld.middleware.jaxb.ObjectFactory;
import com.fjeld.middleware.jaxb.V1Message;
import org.apache.log4j.Logger;

import javax.jms.JMSException;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA.
 * User: fjeld
 * Date: 29/12/2011
 * Time: 16:34
 *
 * Publishes messages.
 */
public class MessagesPublisher extends TopicPublisher implements Runnable
{
    private static final Logger LOGGER = Logger.getLogger(MessagesPublisher.class);
    private int publishInterval;

    public MessagesPublisher(String topicString, String publisherName) throws JMSException
    {
        super(topicString, publisherName);
        LOGGER.info(publisherName);
        publishInterval = 100;
    }

    public void startConnection()
    {
        super.startConnection();
        new Thread(this).start();
    }

    public void run()
    {
        try
        {
            while (true)
            {
                String CID = UUID.randomUUID().toString();

                V1Message v1Message = ObjectFactory.OBJECT_FACTORY.createV1Message();
                v1Message.setUserID("sfjeld");
                v1Message.setCorrespondenceType(V1Message.CorrespondenceType.Login.toString());
                v1Message.setCorrespondenceStatus(V1Message.CorrespondenceStatus.Pending.toString());
                v1Message.setCorrespondenceID(CID);
                v1Message.setPassword("bogusbogus");
                v1Message.setSessionID("session-123456");
                v1Message.setSendingTime(System.currentTimeMillis());
                publish(v1Message);
                Thread.sleep(publishInterval);

                //System.exit(0);

            }
        } catch (Exception e)
        {
            LOGGER.error(e);
        }
    }
}
